var bum = bum || {};
bum.fb = {
	APP_ID : 6077487010,

	ok : false,
	login : false,
	user : null,
	auth : null,

	events : {
		FB_OK : 'FB_OK'
	},
};

bum.fb.init = function() {
	$.ajax({
		async : true,
		type : 'GET',
		url : '//connect.facebook.net/vi_VN/all.js#xfbml=1&appId=' + bum.fb.APP_ID,
		dataType : 'script',
		cache : true,
		data : null,
		complete : function(xhr, status) {
			if (xhr.status == 200) {
				bum.fb.ok = true;
				console.log('Facebook is accessible!');

				window.fbAsyncInit = function() {
					if (FB) {
						FB.init({
							appId : bum.fb.APP_ID,
							logging : false,
							status : true, // Check Facebook Login status
							cookie : true
						});
					}

					bum.listener.fire(bum.fb.events.FB_OK);
				};

			} else {
				console.log('Facebook is not accessible!');
			}
		}
	});
};

bum.fb.getUserInfo = function(callback) {
	console.log('Checking login status');

	if (bum.fb.user != null) {
		console.log('User already login!');
		if (callback && typeof callback == 'function') {
			callback(bum.fb.user);
		}
		return;
	} else {
		console.log('User not login yet');
	}

	// fb available, looking for fb info first
	console.log('Checking Facebook login status');
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			bum.fb.auth = response.authResponse;

			$('body').addClass('fb-authorized');

			FB.api('/' + bum.fb.auth.userID, function(user) {
				user.avatar = 'http://graph.facebook.com/' + user.id + '/picture?type=square';

				bum.fb.user = user;
				bum.fb.login = true;
				if (callback && typeof callback == 'function') {
					callback(bum.fb.user);
				}
			});
		} else {
			if (callback && typeof callback == 'function') {
				callback();
			}
		}
	});
};

bum.fb.login = function(callback, facebook) {
	console.log('Logging user in...');

	// if user already login
	if (bum.fb.user) {
		if (callback && typeof callback == 'function') {
			callback(bum.fb.user);
		}
	}

	// if facebook available
	if (bum.fb.ok
	// && (facebook == null || facebook !== false)
	) {
		FB.login(function(response) {
			console.log('login ok', response);
			if (response.authResponse) {
				console.log("Login with FB successful");
				bum.fb.login = true;
				bum.fb.getUserInfo(function(user) {
					if (callback && typeof callback == 'function') {
						callback(user);
					}
				});
			} else {
				console.log('User cancelled login or did not fully authorize.');
				// bum.fb.login(callback, false);
			}
		}, {
			scope : 'user_games_activity,publish_actions,publish_stream'
		});
		return;
	}
};

bum.fb.logout = function() {
	bum.fb.user = null;

	if (typeof FB == undefined) {
		return;
	}

	FB.logout(function(response) {
		bum.fb.user = null;
		bum.fb.login = false;

		console.log('Thông báo', '<p>Bạn đã đăng xuất thành công!</p>');

		// if (page.getPageType() == 'article' && page.article.commentEnabled()) {
		// $('.commentbox label').html('Bình luận');
		// }
	});
};

// khởi tạo kết nối facebook
$(function() {
	bum.fb.init();
});